
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>

<%@ include file="/common/dateUtil.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<!--添加弹层效果和layui-->
<script type="text/javascript" src="<%=ApplicationPath%>/js/layer/layer.js"></script>
<script>

function viewTerminal()
{
	  var termId = $("#termId").val();
	  InfoWindow.viewTerminal(termId);
}
function newTerminal()
{
	 InfoWindow.viewTerminal(0);
}

function getDate(strDate) {
            var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
             function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
            return date;
        }

/**
 * 根据安装时间，设置服务的开始和截止日期
 */
function setServiceDate(strDate)
{
		//alert(strDate);
		var startDate =  getDate(strDate);
		//alert(startDate.getMonth());
		startDate =  new Date(startDate.getTime()+1* 24 * 60 * 60 * 1000);
		var endDate =  new Date(startDate.getFullYear(), startDate.getMonth(), 0);
		endDate.setMonth(startDate.getMonth()+11);
		//endDate.setDate(0);
			//new Date(startDate.getTime() + 365 * 24 * 60 * 60 * 1000);
		//alert(startDate);

		$("#startDate").val(Utility.dateToString(startDate, "yyyy-MM-dd"));
		$("#endDate").val(Utility.dateToString(endDate,"yyyy-MM-dd"));
}


function doSelectDep()
{
	InfoWindow.selectDep();
}
function onDepSelected(depId,depName)
{
	$("#depId").combotree("setValue", depId);
}

/**
 * 视频存储器下拉框多选时触发
 * @param selectedValues
 */
function onVideoDiskComboTreeChecked(selectedValues)
{
    $("#videoDisk").val(selectedValues);
}


$().ready(function() {
 $("#EntityForm").validate();

 var defaultPlateColor = ${entity.plateColor}; //取出当前车辆的车牌颜色，默认选中
 var defaultVehicleType = "${entity.vehicleType}"; 
 var defaultIndustry = "${entity.industry}"; 
 var defaultTermId = "${entity.termId}";
 var entityId = ${entity.entityId}; 
 if(entityId == 0)
 {
       defaultPlateColor = 2;
	   defaultVehicleType = 20;
	   defaultIndustry = 21;
	   $("#vehicleType").val(defaultVehicleType);
	   $("#industry").val(defaultIndustry);
 }

 var videoDiskArray = [0];
 var strVideoDisk = "${entity.videoDisk}";
 if(strVideoDisk.length > 0) {
     videoDiskArray = strVideoDisk.split(",");
 }

    $("#videoDiskCombo").initComboTree({
        params: {category: "VideoDisk"},
        onCheck: onVideoDiskComboTreeChecked,
        selectedValues: videoDiskArray
    });
	var saveAndNew = ${saveAndNew};
 if(saveAndNew > 0)
	{
	    defaultTermId = "";
	}
 
//ajax填充下拉框数据
 $("#plateColor").lookup({category:"plateColor", selectedValue:defaultPlateColor});
 $("#runStatus").lookup({category:"runStatus", selectedValue:"${entity.runStatus}"});
 $("#useType").lookup({category:"CargoType", selectedValue:"${entity.useType}"});

    $("#vehicleType").lookup({category:"vehicleType", selectedValue:"${entity.vehicleType}"});
 //$("#industry").lookup({category:"industry", selectedValue:"${entity.industry}"});
 //$("#region").lookup({category:"region", selectedValue:""});
 //$("#vehicleType").lookup({category:"vehicleType", selectedValue:"${entity.vehicleType}"});
 //终端列表
 $("#termId").lookup({queryID:"terminal.selectUnbindTerms", selectedValue:defaultTermId, termId:defaultTermId},0);
 $("#memberId").lookup({queryID:"memberInfo.selectMemberList", selectedValue:"${entity.memberId}"},0);

 
		 $("#termType").lookup({category:"TerminalType", selectedValue:"${terminal.termType}"});//终端类型
		 $("#state").lookup({category:"TerminalState", selectedValue:"${terminal.state}"}); //终端状态

    Utility.setComboboxDefaultValue("videoChannelNum", "${entity.videoChannelNum}");
 //创建车组下拉树菜单, 参数是下拉框id和 默认的部门id
	Utility.createDepTree("depId", ${entity.depId});

	 //创建行业类型下拉树菜单
	 var url = "<%=ApplicationPath%>/basicData/comboTree.action?queryID=selectIndustryType";
	 Utility.createComboTree('industry', url, defaultIndustry);
		 //创建车籍地区下拉树菜单
	 var url = "<%=ApplicationPath%>/basicData/comboTree.action?queryID=selectRegion";
	 var defaultRegion = "${entity.region}";
	 Utility.createComboTree('region', url, defaultRegion);
	
	 //创建车辆类型下拉树菜单
	 //var url = "<%=ApplicationPath%>/basicData/comboTree.action?queryID=selectVehicleType";
	 //var defaultRegion = "${entity.region}";
	 //Utility.createComboTree('vehicleType', url, defaultVehicleType);

	$("#termId").change(function()
	{
		//当什么也不选择的时候，就是解除绑定,更新车辆绑定终端Id为0
		  var val = $("#termId option:selected").val();
		  //alert(val);
		  if(val.length == 0)
			{
				//$("#termId option:selected").val("0");
				$("#termNo").val(""); //清空录入的数据
				return;
			}

			var url = "<%=ApplicationPath%>/vehicle/getTerminalInfo.action";
			  $.getJSON(url, {terminalId:val}, 
			         function(result){		
				         if(result.success)
						  {
							  var term = result.data;
							  $("#termNo").val(term.termNo);
							  $("#termType").val(term.termType);
							  $("#state").val(term.state);
							  var installTime = term.installTime;
							  if(installTime.length > 10)
							  {
								  installTime = installTime.substring(0,10);
							  }
							  $("#installTime").val(installTime);
						  }else
						  {
							  //后台出错
							  alert(result.message);
						  }
			  });
	});

	var message = "${message}";
	if(message.length > 0)
	{
		//保存成功后，刷新主窗口数据
         window.parent.refreshDataWindow();
		 //如果是保存并新增，则需要清空关键数据
		 if(saveAndNew  && message.indexOf("成功")>0)
		{
			$("#entityId").val("0");
			$("#plateNo").val("");
			$("#simNo").val("");
			$("#termNo").val("");
			$("#termId").val("");
			$("#telephone").val("");
			$("#monitor").val("");
			$("#driverName").val("");
		}
	}
	//保存并新增按钮点击
	$("#btnNew").click(function()
	{
		$("#saveAndNew").val("true");
		$("#btnSubmit").click();
	});
	//安装时间设置后，需要重新设置服务开始和截止日期
	jQuery('#installTime').datetimepicker({
			 format:'Y-m-d',
	  timepicker:false,
	  onChangeDateTime:function(dp,$input){
		
           var strDate = $input.val();
		   setServiceDate(strDate);		
	  }
	});

	//如果是新增车辆，初始化服务开始日期和结束日期
	if(entityId == 0)
	{
	var installTime = $("#installTime").val();
	setServiceDate(installTime);
	}


	//设置显示车辆视频通道信息
	setChannels();
});

var setChannels = function(){
	try {
		var entityId = parseInt("${entity.entityId }");
		// vehicleChannelsNum = ${entity.videoChannelNum};
		// console.log("开始设置通道信息 , vehicleId = " + entityId);
		if(!isNaN(entityId) && entityId > 0){
			getChannelsDataStr(entityId);
		}
	} catch (error) {
		console.error("设置通道信息错误 ： "+error);
	}
	
};
//获取车辆视频通道保存字符串
var getChannelsDataStr = function(vehicleId){	
	var url = "<%=ApplicationPath%>/vehicleRealTimeVideo/getChannelDataStr.action";
	var param = {
		vehicleId:vehicleId,
	};
	Utility.getAjaxData(url,JSON.stringify(param),function(data){
		// console.log("获取到数据库中的通道信息数据  ： ");
		// console.log(data);
		if(data.success){
			//查询成功
			if(data.data.length >0){
				vehicleChannelsDataStr = data.data[0].channelsDataStr;
				vehicleChannelsDataStrExitFlg = true;
			}
		}
		//不论是否获取数据库成功，都显示设置页面
		// console.log("当前车辆的设置信息为：" + vehicleChannelsDataStr);
		
		//更新显示信息
		if(vehicleChannelsDataStr == ""){
			$("#vehicleChannelData").html("默认通道：无");
		}else{
			$("#vehicleChannelData").html("默认通道："+vehicleChannelsDataStr);
		}
	});
}


/*
设置车辆视频通道信息
	1.从数据库获取设置信息
	2.显示选择页面，并显示选择的通道信息
	3.设定并保存

*/
var vehicleChannelsDataStr = "";
var vehicleChannelsDataStrExitFlg = false;
var vehicleChannelsNum = 0;
var showSetChannelsDataPage = function(){
	var vehicleId = parseInt("${entity.entityId}");
	if(isNaN(vehicleId) || vehicleId == 0){
		alert("请添加完车辆并保存后再设置默认通道");
		return;
	}
	vehicleChannelsNum = parseInt($("#videoChannelNum").val());
	if(isNaN(vehicleChannelsNum)){
		vehicleChannelsNum = 0;
	}
	// console.log("当前通道数量为："+vehicleChannelsNum);
	layer.open({
            title: '选择视频通道',
            type: 2,
            area: ['480px', '260px'],
            fixed: false, //不固定
            // offset:['150px','450px'],
            maxmin: false,
            content: '<%=ApplicationPath%>/vehicleRealTimeVideo/editDefaultChannels.action',
            cancel: function () {
                // layer.msg('没有进行任何操作就关闭');
            }
        });
} 
//保存设置信息
var saveChannelsDataStr = function(newStr){
	// console.log("开始保存新的信息 : "+newStr + " , 原有信息为 ：" + vehicleChannelsDataStr);
	var updateFlg = "";
	var simNoTemp = "${entity.simNo}";
	var entityId = "${entity.entityId}";
	var plateNo = "${entity.plateNo}";
	var param = {
		vehicleId:entityId,
		simNo:simNoTemp,
		channelsDataStr:newStr,
		plateNo:plateNo,
	};
	var url = "";
	if(vehicleChannelsDataStr == "" && !vehicleChannelsDataStrExitFlg) {//数据库不存在当前数据
		url = "<%=ApplicationPath%>/vehicleRealTimeVideo/insertChannelDataStr.action";
		updateFlg = "插入 ..";
	}else{
		url = "<%=ApplicationPath%>/vehicleRealTimeVideo/updateChannelDataStr.action";
		updateFlg = "更新 ..";
	}
	
	// console.log("更新请求数据地址为 ："+ url + " , 参数为 ： ");
	// console.log(param);

	Utility.getAjaxData(url,JSON.stringify(param),function(data){
		// console.log(updateFlg + "通道数据请求返回的结果  ： ");
		// console.log(data);
		if(data.success){
			console.log( updateFlg + "通道数据成功!");

			//修改当前保存的信息
			vehicleChannelsDataStr = newStr;
			//显示修改后的信息
			if(newStr == ""){
				$("#vehicleChannelData").html("默认通道：无");
			}else{
				$("#vehicleChannelData").html("默认通道："+newStr);
			}
		}else{
			console.error( updateFlg + "通道数据失败, : "+data.message);
		}
	});

}
</script>
 <BODY>
	<form id="EntityForm" name="EntityForm" 
			action='<c:url value="/vehicle/save.action" />' method="POST">
				<input type="hidden" id="entityId" name="entity.entityId"
					value="${entity.entityId}"/>
					<input type="hidden" id="saveAndNew" name="saveAndNew"
					value="false"/>
		<input type="hidden" id="videoDisk" name="entity.videoDisk"
			   value="${entity.videoDisk}"/>
  <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="4" style="font-weight: bold; background: #EFEFEF;" height="25">车辆信息
						<span class="MessageFromServer">${message}</span>
						</td>
					</tr>
					<tr>
						<td align="right">车牌号码:</td>
						<td align="left"><input  id="plateNo" value="${entity.plateNo}"
						name="entity.plateNo" class="required" maxlength="10" maxlength="11" size="20"> 
						</td>
						<td align="right">车牌颜色:</td>
						<td><select id="plateColor"  style="width: 150px;" name="entity.plateColor" class="required">
						   </select>

							</td>
					</tr>
					<tr>
						
						<td align="right">所属业户:</td>
						<td align="left"><select id="memberId" name="entity.memberId"></select>
						</td>
						
						<td align="right">车辆组:</td>
						<td align="left">
							<select id="depId"  style="width: 150px;" name="entity.depId" value="" class="required">
							</select>
							<a id="btnSelectVehicle" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="doSelectDep();"></a>
						</td>
						
					</tr>

					<tr>
					<td align="right">行业类型:</td>
						<td align="left">
							<select id="industry"  style="width: 150px;" name="entity.industry" >
						</td>

						<td align="right">终端卡号
							:</td>
						<td><input type="text" name="entity.simNo" size="20" maxlength="11" value="${entity.simNo}" id="simNo" class="required"></td>
					</tr>
							<tr>
						<td align="right">车籍地:</td>
						<td align="left">
						
							<select id="region"  style="width: 150px;" name="entity.region" >
						</td>
						<td align="right">绑定终端:
							</td>
						<td><select name="entity.termId" id="termId" style="width: 150px;">

                             </select>
							 <input type="button" class="button gray medium" value="编辑" onclick="viewTerminal();">
							
						</td>
					</tr>

					<tr>				
				<td align="right">唯一终端ID号
					:</td>
				<td align="left"><input type="text" 
					id="termNo" value="${terminal.termNo}"
					name="terminal.termNo" maxLength="7"
					size="20" class="{required:true,maxlength:7}"  /> <span class="star">*</span>
				</td>
				<td align="right">终端类型
					:</td>
				<td>
				<select name="terminal.termType" id="termType" style="width:150px" class="required">  
				</select>
					</td>
			</tr>
			
			<tr>
			<td align="right">设备当前状态:</td>
				<td align="left"><select name="terminal.state" id="state" style="width:150px;" class="required">
</select>
				</td>
				<td align="right">安装时间:</td>
				<td align="left">
							  <input type="text"  id="installTime"  name="terminal.installTime"   value='<fmt:formatDate pattern="yyyy-MM-dd" value="${terminal.installTime}" />' readonly=false></input>
				</td>
				</tr>

					<tr>

						<td align="right">视频通道数:</td>
						<td align="left"><select id="videoChannelNum" name="entity.videoChannelNum" style="width: 50px;">
							<option value="0" >无视频</option>
							<option value="1" >1 </option>
							<option value="2" >2 </option>
							<option value="3" >3 </option>
							<option value="4" >4 </option>
							<option value="5" >5 </option>
							<option value="6" >6 </option>
							<option value="7" >7 </option>
							<option value="8" >8 </option>
							<option value="9" >9 </option>
							<option value="10" >10 </option>
							<option value="11" >11 </option>
							<option value="12" >12 </option>
							<option value="13" >13 </option>
							<option value="14" >14 </option>
							<option value="15" >15 </option>
							<option value="16" >16 </option>
							<option value="17" >17 </option>
							<option value="18" >18 </option>
							<option value="19" >19 </option>
							<option value="20" >20 </option>
							<option value="21" >21 </option>
							<option value="22" >22 </option>
							<option value="23" >23 </option>
							<option value="24" >24 </option>
							<option value="25" >25 </option>
							<option value="26" >26 </option>
							<option value="27" >27 </option>
							<option value="28" >28 </option>
							<option value="29" >29 </option>
							<option value="30" >30 </option>
							<option value="31" >31 </option>
							<option value="32" >32 </option>

						</select>
						<a  href="javascript:void(0);" onclick="javascript:showSetChannelsDataPage();">&nbsp;&nbsp;<span id="vehicleChannelData">默认通道：</span></a>
						</td>
						
						<td align="right">存储器
							:</td>
						<td>
						
							<select id="videoDiskCombo"  style="width: 150px;" name="videoDiskCombo" >
							</select>
						</td>
						
					</tr>

					<tr>
						<td align=right>主司机姓名:</td>
				<td align="left">
					<input type="text" name="vehicleDriver.driverName" size="20" maxlength="10"   value="${vehicleDriver.driverName}" id="driverName" /><span
						class=""></span>
				</td>
					
				<td align=right>押运员姓名:</td>
				<td align="left"><input type="text" name="vehicleDriver.monitor" size="20" maxlength="10" value="${vehicleDriver.monitor}" id="monitor" class=""  /></td>

					</tr>

					<tr>
					<td  align=right>司机联系电话:</td>
				<td align="left"><input type="text" name="vehicleDriver.mobilePhone" size="20" maxlength="12" value="${vehicleDriver.mobilePhone}" id="telephone" class="digist" > </td>

						    	<td align="right">运行状态:
							:</td>
						<td>
						<select id="runStatus" name="entity.runStatus" class="required"></select>
					
						</td>
					</tr>
					<tr>
						<td align="right">车辆类型
							:</td>
						<td>

							<select id="vehicleType"  style="width: 150px;" name="entity.vehicleType" ></select>
						</td>
						<td align="right">发动机号:</td>
						<td align="left"><input  id="motorID" value="${entity.motorID}"
												 name="entity.motorID"  maxlength="60"  size="20">
						</td>

					</tr>
					<!--
					<tr>
						
						<td align="right">服务开始时间:</td>
						<td>
						  <input type="text" name="entity.startDate" id="startDate" class="datepicker" value='<fmt:formatDate pattern="yyyy-MM-dd" value="${entity.startDate}" />'></input>
						</td>
<td align="right">服务结束时间:</td>
						<td align="left">
							  <input type="text" name="entity.endDate" id="endTime" class="datepicker" value='<fmt:formatDate pattern="yyyy-MM-dd" value="${entity.endDate}" />'></input>
							
						</div>
						</td>

					</tr>
					-->
			
					
					<tr>
						<td colspan="4" align="center">
							<div align="center">

								<button type="submit" class="button green medium" ><i class="fa fa-check" style="margin-right:3px;"></i> 保存</button>

								<button type="submit" id="btnNew" class="button blue medium" ><i class="fa fa-check" style="margin-right:3px;"></i> 保存并新增</button>

								<!--
								 <input type="button" class="button gray medium" value="添加终端"  onclick="newTerminal();">
								 -->
							</div>
							
							<div align="center" style="color:red;">
							    (如果修改车牌号和卡号，要重新刷新主页面）
							</div>
							
							</td>
					</tr>
				</tbody></table>
 </BODY>
</HTML>
